package com.neusoft.nemp.controller;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LoginViewController {
    @FXML
    private RadioButton rdoMale,rdoFemale;
    @FXML
    private ToggleGroup genderGroup;
    @FXML
    private ComboBox<String> cmbProvince,cmbCity,cmbDistrict;
    @FXML
    private CheckBox chkMusic,chkSport,chkGame;

    private Map<String, List<String>> provinceMap = new HashMap<>();
    private Map<String,List<String>> cityMap = new HashMap<>();

    @FXML
    public void initialize(){
        genderGroup = new ToggleGroup();//创建一个单选的选项组
        rdoMale.setToggleGroup(genderGroup);//讲单选按钮设置到同一个组中
        rdoFemale.setToggleGroup(genderGroup);
        //初始化下拉列表数据
        provinceMap.put("辽宁省",List.of("沈阳市","大连市"));
        provinceMap.put("广东省",List.of("广州市","深圳市"));
        cityMap.put("沈阳市",List.of("和平区","浑南区","沈河区"));
        cityMap.put("大连市",List.of("中山区","沙河口区","高新区"));
        cityMap.put("广州市",List.of("天河区","越秀区"));
        cityMap.put("深圳市",List.of("南山区","福田区"));
        cmbProvince.setItems(FXCollections.observableArrayList(provinceMap.keySet()));
        cmbProvince.getSelectionModel().clearSelection();
        cmbProvince.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
                List<String> citys = provinceMap.getOrDefault(newValue,List.of());
                cmbCity.setItems(FXCollections.observableArrayList(citys));
                cmbCity.getSelectionModel().clearSelection();
                cmbDistrict.getItems().clear();
            }
        });
        cmbCity.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observableValue, String oldValue, String newValue) {
                List<String> districts = cityMap.getOrDefault(newValue,List.of());
                cmbDistrict.setItems(FXCollections.observableArrayList(districts));
                cmbDistrict.getSelectionModel().clearSelection();
            }
        });
    }

    public void login(){
        //获取下拉列表的值
        String province = cmbProvince.getValue();
        String city = cmbCity.getValue();
        String district = cmbDistrict.getValue();
        //获取单选按钮的值
        String gender = ((RadioButton)genderGroup.getSelectedToggle()).getText();

        System.out.println(province +":" + city +":" + district);
        System.out.println("gender:" + gender);
        if(chkMusic.isSelected()){
            System.out.println("爱好音乐");
        }
        if(chkGame.isSelected()){
            System.out.println("爱好游戏");
        }
        if(chkSport.isSelected()){
            System.out.println("爱好运动");
        }
    }
}
